package com.gym.code.generator.impl;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;

import com.gym.code.config.admin.bean.method.MethodBaseMappingBean.MethodParameterCombineType;
import com.gym.code.generator.EnumContants.FieldClassType;
import com.gym.code.generator.model.mapping.field.EnumFieldGenerateModel;
import com.gym.code.generator.model.mapping.field.FieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterFieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterListValueFieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterRangeFieldGenerateModel;
import com.gym.code.generator.model.mapping.layer.LayerGenerateModel;
import com.gym.code.generator.model.mapping.method.AbstractMethodGenerateModel;
import com.gym.code.generator.model.mapping.method.MethodUpdateModel;
import com.gym.code.generator.utils.LocalStringUtils;
/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-5-23 下午8:46:38 
 * @param <T>
 */
public abstract class AbstractImplGenerator<T extends LayerGenerateModel> extends AbstractFaceGenerator<T> {
	@Override
	protected Set<String> doGenerateImport(LayerGenerateModel bean) {
		Set<String> result = super.doGenerateImport(bean);
		result.addAll(generateCommonSpringImport());
		result.addAll(generateCommonJavaImport());
		result.addAll(generateFaceImport(bean));
		return result;
	}
	
	protected final Set<String> generateCommonSpringImport() {
		Set<String> result = new HashSet<String>();
		result.add("org.springframework.beans.factory.annotation.Qualifier");
		result.add("org.springframework.beans.factory.annotation.Autowired");
		result.addAll(generateSpringImport());
		return result;
	}
	protected abstract Set<String> generateSpringImport();
	
	protected final Set<String> generateCommonJavaImport(){
		Set<String> result = super.generateCommonJavaImport();
		result.addAll(generateJavaImport());
		return result;
	}
	protected abstract Set<String> generateJavaImport();
	
	
	protected abstract Set<String> generateFaceImport(LayerGenerateModel model);
	
	
	protected String generateSelectParams(AbstractMethodGenerateModel methodModel) {
		   StringBuilder result = new StringBuilder();
		   if(CollectionUtils.isEmpty(methodModel.getParameterList())){
			   return result.toString();
		   }
		   if(methodModel.getMethodParameterType() == MethodParameterCombineType.fixedParamter) {
			   //要修改的字段在前
			   if(methodModel instanceof MethodUpdateModel){
				   MethodUpdateModel updateMethodModel = (MethodUpdateModel)methodModel;
				   for(FieldGenerateModel item : updateMethodModel.getUpdateFieldList()){
					   result.append(wrapParams(item));
				   }
			   }
			   //条件在后
			   for(ParameterFieldGenerateModel item : methodModel.getParameterList()) {
				   result.append(wrapParams(item));
			   }
			   
		   }else {
			 //要修改的字段在前
			   if (methodModel instanceof MethodUpdateModel) {
				   MethodUpdateModel updateMethodModel = (MethodUpdateModel) methodModel;
				   for (FieldGenerateModel item : updateMethodModel.getUpdateFieldList()) {
					   if (item instanceof EnumFieldGenerateModel && ((EnumFieldGenerateModel)item).getEnumGenerateModel() != null) {
						   result.append("bean.get").append(item.getPropertyNameByFirstLetterUpper()).append("().getId(),");
					   } else {
						   result.append("bean.get").append(item.getPropertyNameByFirstLetterUpper()).append("(),");
					   }
				   }
			   }
			 //条件在后
			   for(ParameterFieldGenerateModel item : methodModel.getParameterList()) {
				   if(item.getEnumGenerateModel() == null){
					   result.append("bean.get").append(item.getPropertyNameByFirstLetterUpper()).append("(),");
				   } else {
					   result.append("bean.get").append(item.getPropertyNameByFirstLetterUpper()).append("().getId(),");
				   }
			   }
		   }
		   return LocalStringUtils.removeLastChar(result.toString(), ',');
		}
	
	

	private StringBuilder wrapParams (FieldGenerateModel item ) {
		StringBuilder result = new StringBuilder();
		   if(item instanceof ParameterListValueFieldGenerateModel) {
			   if(FieldClassType.isNumerType(item.getValueType())) {
				   result.append("StringUtils.join("+item.getPropertyName()+ ",\",\")").append(",");
			   } else {
				   result.append("\"'\"+").append("StringUtils.join("+item.getPropertyName()+ ",\"','\")").append("+\"'\"");
				   result.append(",");
			   }
		   } else if(item instanceof ParameterRangeFieldGenerateModel){
			   result.append(((ParameterRangeFieldGenerateModel) item).getBeginName()).append(",");
			   result.append(((ParameterRangeFieldGenerateModel) item).getEndName()).append(",");
		   } else {
			   result.append(item.getPropertyName()).append(",");
		   }
		   return result;
	}
}
